package com.kla.power.controller;

import com.alibaba.excel.EasyExcel;
import com.kla.power.entity.InverterAmountStatisticsDayEntity;
import com.kla.power.service.IInverterAmountStatisticsDayService;
import com.kla.power.util.ResultBody;
import com.kla.power.vo.DownDay;
import com.kla.power.vo.DownMonth;
import com.kla.power.vo.DownYear;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 逆变器电量统计，每日凌晨更新昨日发电量，用于显示逆变器详情页 前端控制器
 * </p>
 * @author WuZH
 * @since 2024-06-02
 */
@RestController
@RequestMapping ("/InverterAmountStatistics")
@CrossOrigin (value = "*", maxAge = 3600)
public class InverterAmountStatisticsDayController {

    @Resource
    private IInverterAmountStatisticsDayService iInverterAmountStatisticsDayService;

    @RequestMapping ("day")
    public ResultBody<InverterAmountStatisticsDayEntity> day (@RequestBody InverterAmountStatisticsDayEntity inverterAmountStatisticsDayEntity) {
        return iInverterAmountStatisticsDayService.queryDay ( inverterAmountStatisticsDayEntity );
    }

    @RequestMapping ("month")
    public ResultBody month (@RequestBody InverterAmountStatisticsDayEntity inverterAmountStatisticsDayEntity) {
        List<InverterAmountStatisticsDayEntity> inverterAmountStatisticsDayEntities = iInverterAmountStatisticsDayService.queryMonth ( inverterAmountStatisticsDayEntity );
        return ResultBody.success ( inverterAmountStatisticsDayEntities );
    }

    @RequestMapping ("year")
    public ResultBody year (@RequestBody InverterAmountStatisticsDayEntity inverterAmountStatisticsDayEntity) {
        List<InverterAmountStatisticsDayEntity> inverterAmountStatisticsDayEntities = iInverterAmountStatisticsDayService.queryYear ( inverterAmountStatisticsDayEntity );
        return ResultBody.success ( inverterAmountStatisticsDayEntities );
    }

    @RequestMapping ("downDay")
    public void downDay (@RequestBody InverterAmountStatisticsDayEntity inverterAmountStatisticsDayEntity, HttpServletResponse response) throws IOException {
        List<InverterAmountStatisticsDayEntity> inverterAmountStatisticsDayEntities = iInverterAmountStatisticsDayService.downDay ( inverterAmountStatisticsDayEntity );
        List<DownDay> downDays = new ArrayList<> ();
        for ( InverterAmountStatisticsDayEntity amountStatisticsDayEntity : inverterAmountStatisticsDayEntities ) {
            DownDay downDay = new DownDay ();
            downDay.setDayPower ( amountStatisticsDayEntity.getYesterdayPower () );
            downDay.setDeviceName ( amountStatisticsDayEntity.getDeviceTitle () );
            downDay.setStationName ( amountStatisticsDayEntity.getStationName () );
            downDay.setPower ( amountStatisticsDayEntity.getPower () );
            downDay.setDeviceSn ( amountStatisticsDayEntity.getInverterSn () );
            downDays.add ( downDay );
        }
        // 指定文件名和表头信息写入Excel
        String fileName = "students.xlsx";
        // 设置响应头信息
        response.setContentType ( "application/vnd.ms-excel" );
        response.setCharacterEncoding ( "utf-8" );
        response.setHeader ( "Content-disposition", "attachment;filename=" + fileName + ".xlsx" );
        // 使用EasyExcel写入数据到输出流
        SimpleDateFormat s = new SimpleDateFormat ("yyyy-MM-dd");
        EasyExcel.write ( response.getOutputStream (), DownDay.class ).sheet ( s.format ( inverterAmountStatisticsDayEntity.getCreateTime () ) ).doWrite ( downDays );
    }

    @RequestMapping ("downMonth")
    public void downMonth (@RequestBody InverterAmountStatisticsDayEntity inverterAmountStatisticsDayEntity, HttpServletResponse response) throws IOException {
        List<InverterAmountStatisticsDayEntity> inverterAmountStatisticsDayEntities = iInverterAmountStatisticsDayService.queryMonth ( inverterAmountStatisticsDayEntity );
        List<DownMonth> downMonths = new ArrayList<> ();
        for ( InverterAmountStatisticsDayEntity amountStatisticsDayEntity : inverterAmountStatisticsDayEntities ) {
            DownMonth downMonth = new DownMonth ();
            downMonth.setDeviceName ( amountStatisticsDayEntity.getDeviceTitle () );
            downMonth.setStationName ( amountStatisticsDayEntity.getStationName () );
            downMonth.setPower ( amountStatisticsDayEntity.getPower () );
            downMonth.setDeviceSn ( amountStatisticsDayEntity.getInverterSn () );
            List<Double> powers = amountStatisticsDayEntity.getPowers ();
            //----
            downMonth.setNum1 ( String.valueOf ( powers.get ( 0 ) ) );
            downMonth.setNum2 ( String.valueOf ( powers.get ( 1 ) ) );
            downMonth.setNum3 ( String.valueOf ( powers.get ( 2 ) ) );
            downMonth.setNum4 ( String.valueOf ( powers.get ( 3 ) ) );
            downMonth.setNum5 ( String.valueOf ( powers.get ( 4 ) ) );
            downMonth.setNum6 ( String.valueOf ( powers.get ( 5 ) ) );
            downMonth.setNum7 ( String.valueOf ( powers.get ( 6 ) ) );
            downMonth.setNum8 ( String.valueOf ( powers.get ( 7 ) ) );
            downMonth.setNum9 ( String.valueOf ( powers.get ( 8 ) ) );
            downMonth.setNum10 ( String.valueOf ( powers.get ( 9 ) ) );
            downMonth.setNum11 ( String.valueOf ( powers.get ( 10 ) ) );
            downMonth.setNum12 ( String.valueOf ( powers.get ( 11 ) ) );
            downMonth.setNum13 ( String.valueOf ( powers.get ( 12 ) ) );
            downMonth.setNum14 ( String.valueOf ( powers.get ( 13 ) ) );
            downMonth.setNum15 ( String.valueOf ( powers.get ( 14 ) ) );
            downMonth.setNum16 ( String.valueOf ( powers.get ( 15 ) ) );
            downMonth.setNum17 ( String.valueOf ( powers.get ( 16 ) ) );
            downMonth.setNum18 ( String.valueOf ( powers.get ( 17 ) ) );
            downMonth.setNum19 ( String.valueOf ( powers.get ( 18 ) ) );
            downMonth.setNum20 ( String.valueOf ( powers.get ( 19 ) ) );
            downMonth.setNum21 ( String.valueOf ( powers.get ( 20 ) ) );
            downMonth.setNum22 ( String.valueOf ( powers.get ( 21 ) ) );
            downMonth.setNum23 ( String.valueOf ( powers.get ( 22 ) ) );
            downMonth.setNum24 ( String.valueOf ( powers.get ( 23 ) ) );
            downMonth.setNum25 ( String.valueOf ( powers.get ( 24 ) ) );
            downMonth.setNum26 ( String.valueOf ( powers.get ( 25 ) ) );
            downMonth.setNum27 ( String.valueOf ( powers.get ( 26 ) ) );
            downMonth.setNum28 ( String.valueOf ( powers.get ( 27 ) ) );
            downMonth.setNum29 ( String.valueOf ( powers.get ( 28 ) ) );
            downMonth.setNum30 ( String.valueOf ( powers.get ( 29 ) ) );
            downMonth.setNum31 ( String.valueOf ( powers.get ( 30 ) ) );
            downMonths.add ( downMonth );
        }
        // 指定文件名和表头信息写入Excel
        String fileName = "students.xlsx";
        // 设置响应头信息
        response.setContentType ( "application/vnd.ms-excel" );
        response.setCharacterEncoding ( "utf-8" );
        response.setHeader ( "Content-disposition", "attachment;filename=" + fileName + ".xlsx" );
        // 使用EasyExcel写入数据到输出流
        SimpleDateFormat s = new SimpleDateFormat ("yyyy-MM");
        EasyExcel.write ( response.getOutputStream (), DownMonth.class ).sheet ( s.format ( inverterAmountStatisticsDayEntity.getMonthDate () ) ).doWrite ( downMonths );
    }

    @RequestMapping ("downYear")
    public void downYear (@RequestBody InverterAmountStatisticsDayEntity inverterAmountStatisticsDayEntity, HttpServletResponse response) throws Exception {
        List<InverterAmountStatisticsDayEntity> inverterAmountStatisticsDayEntities = iInverterAmountStatisticsDayService.queryYear ( inverterAmountStatisticsDayEntity );
        List<DownYear> downYears = new ArrayList<> ();
        for ( InverterAmountStatisticsDayEntity amountStatisticsDayEntity : inverterAmountStatisticsDayEntities ) {
            DownYear downYear = new DownYear ();
            downYear.setDeviceName ( amountStatisticsDayEntity.getDeviceTitle () );
            downYear.setStationName ( amountStatisticsDayEntity.getStationName () );
            downYear.setPower ( amountStatisticsDayEntity.getPower () );
            downYear.setDeviceSn ( amountStatisticsDayEntity.getInverterSn () );
            List<Double> powers = amountStatisticsDayEntity.getPowers ();
            //------
            downYear.setMonth1 ( String.valueOf ( powers.get ( 0 ) ) );
            downYear.setMonth2 ( String.valueOf ( powers.get ( 1 ) ) );
            downYear.setMonth3 ( String.valueOf ( powers.get ( 2 ) ) );
            downYear.setMonth4 ( String.valueOf ( powers.get ( 3 ) ) );
            downYear.setMonth5 ( String.valueOf ( powers.get ( 4 ) ) );
            downYear.setMonth6 ( String.valueOf ( powers.get ( 5 ) ) );
            downYear.setMonth7 ( String.valueOf ( powers.get ( 6 ) ) );
            downYear.setMonth8 ( String.valueOf ( powers.get ( 7 ) ) );
            downYear.setMonth9 ( String.valueOf ( powers.get ( 8 ) ) );
            downYear.setMonth10 ( String.valueOf ( powers.get ( 9 ) ) );
            downYear.setMonth11 ( String.valueOf ( powers.get ( 10 ) ) );
            downYear.setMonth12 ( String.valueOf ( powers.get ( 11 ) ) );
            //------
            downYears.add ( downYear );
        }
        // 指定文件名和表头信息写入Excel
        String fileName = "students.xlsx";
        // 设置响应头信息
        response.setContentType ( "application/vnd.ms-excel" );
        response.setCharacterEncoding ( "utf-8" );
        response.setHeader ( "Content-disposition", "attachment;filename=" + fileName + ".xlsx" );
        // 使用EasyExcel写入数据到输出流
        EasyExcel.write ( response.getOutputStream (), DownYear.class ).sheet ( inverterAmountStatisticsDayEntity.getYear ().concat ( "年" ) ).doWrite ( downYears );
    }
}
